<template>
    <div>
        <div class="head">
            <span class="iconfont icon-arrow-left-bold" @click="gotoDetail"></span>
            <div class="title">{{ title }}</div>
        </div>
        <div class="video">
            <video :poster="img" :src="video" controls="controls"></video>
        </div>
        <van-tabs v-model="activeName">
            <van-tab title="目录" name="b">
                <div class="introduce">
                    <introducom :section1="section1" v-for="(item, index) in tabtitle" :key="index" :title="item"></introducom>
                </div>
            </van-tab>
            <van-tab title="评论" name="c">
                <commenttab :key="rekey" :comments="comments" :users="users"></commenttab>
                <div class="footer">
                    <div class="input">
                        <input v-model="text" type="text" placeholder="请输入评论内容" />
                    </div>
                    <div class="button" @click="issue">发布</div>
                </div>
            </van-tab>
        </van-tabs>
    </div>
</template>

<script>
import Vue from "vue";
import { Tab, Tabs } from "vant";
import { Toast } from "vant";
Vue.use(Tab).use(Tabs);
import introducom from "@/components/IntroduCom.vue";
import commenttab from "@/components/CommentTab.vue";
export default {
    data() {
        return {
            title: "",
            img: "",
            video: "",
            activeName: "a",
            section: [],
            section1: [],
            tabtitle: {},
            // 详情页ID
            id: "",
            rekey: true,
            text: "",
            // 评论数据
            comments: [],
            users: [],
        };
    },
    components: {
        introducom,
        commenttab,
    },
    methods: {
        gotoDetail() {
            this.$router.push({
                path: "/vod/detail",
                query: {
                    id: this.id,
                },
            });
        },
        issue() {
            if (!this.text) {
                Toast.fail("不能为空");
                return;
            }
            this.$request.post("/api/v2/video/" + this.$route.query.id + "/comment", {
                content: this.text,
            });
            this.rekey = !this.rekey;
            this.text = "";
            setTimeout(() => {
                this.$request
                    .get(`/api/v2/video/${this.$route.query.id}/comments`, {
                        params: {},
                    })
                    .then((res) => {
                        this.comments = res.data.comments;
                        this.user = res.data.users;
                    });
            }, 1000);
        },
    },
    async created() {
        let token = localStorage.getItem("h5-token");
        if (token) {
            this.token = token;
            await this.$request.get("/api/v2/member/detail", {
                params: {
                    Authorization: `Bearer ${token}`,
                },
            });
        }
        let res = await this.$request.get("/api/v2/video/" + this.$route.query.id, {
            params: {},
        });
        // 页面标题
        this.title = res.data.video.title;
        this.img = res.data.course.thumb;
        let vio = await this.$request.get("/api/v2/video/" + this.$route.query.id + "/playinfo", {
            params: {
                is_try: 0,
            },
        });
        this.video = vio.data.urls[0].url;
        this.id = res.data.course.id;
        // 列表数据
        this.section = res.data.videos;
        this.tabtitle = res.data.chapters;
        for (var i in this.section) {
            this.section1.push(this.section[i]);
        }
        //评论数据
        let comment = await this.$request.get("/api/v2/video/" + this.$route.query.id + "/comments", {
            params: {},
        });
        this.users = comment.data.users;
        this.comments = comment.data.comments;
    },
};
</script>

<style lang="less" scoped>
/deep/ .van-tabs__wrap {
    height: 55px;
    box-shadow: 0 2px 4px 0 hsl(0deg 0% 80% / 20%);
}

/deep/ .van-tabs__nav {
    z-index: 999;
}

/deep/ .van-tabs__line {
    bottom: 25px;
    width: 20px;
    height: 3px;
    background: #3ca7fa;
    border-radius: 3px;
}

/deep/ .van-tab--active {
    span {
        font-weight: 600;
        color: #3ca7fa;
    }
}
.head {
    position: fixed;
    top: 0;
    width: 100%;
    height: 50px;
    background-color: #fff;
    z-index: 100;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #f1f2f6;
    span {
        font-size: 19px;
        position: absolute;
        top: 50%;
        margin-top: -10px;
        left: 4%;
    }
    .title {
        flex: 1;
        font-size: 17px;
        font-weight: 500;
        text-align: center;
        color: #333;
        line-height: 22px;
        overflow: hidden;
    }
}
.video {
    margin-top: 50px;
    width: 100%;
    height: 250px;
    background: #000;
    video {
        width: 100%;
        height: 100%;
    }
}
.introduce {
    width: 100%;
    color: #333;
    line-height: 21px;
    padding: 10px 15px 15px 15px;
    .introduce-item {
        width: 100%;
        .introduce-title {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 15px;
            color: #333;
            font-weight: 700;
            margin-bottom: 20px;
            margin-top: 10px;
        }
        .introduce-box {
            width: 100%;
            .box-item {
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-bottom: 10px;
                line-height: 36px;
                padding: 0 10px 0 10px;
                background: #f6f6f6;
                border-radius: 4px;
                .item-title {
                    flex: 1;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    font-size: 14px;
                    color: #333;
                    span:nth-child(1) {
                        padding: 2px 4px;
                        background-color: #04c877;
                        border-radius: 2px;
                        color: #fff;
                        font-size: 12px;
                        margin-right: 10px;
                    }
                    span:nth-child(2) {
                        flex: 1;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        font-size: 14px;
                        color: #333;
                    }
                }
                .item-time {
                    width: auto;
                    height: auto;
                    font-size: 12px;
                    margin-left: 10px;
                    color: #999;
                }
            }
        }
    }
}
.footer {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 11;
    height: 53px;
    background-color: #fff;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    display: flex;
    padding-left: 15px;
    padding-right: 15px;
    .input {
        flex: 1;
        padding-top: 10px;
        input {
            width: 100%;
            height: 22px;
            font-size: 14px;
            font-weight: 400;
            color: #333;
            line-height: 22px;
            margin-top: 8px;
            border: none;
        }
    }
    .button {
        width: auto;
        height: 33px;
        font-size: 14px;
        font-weight: 400;
        color: #666;
        line-height: 33px;
        padding-left: 15px;
        padding-top: 10px;
    }
    .collect {
        width: 25px;
        margin-right: 15px;
        span {
            display: block;
            margin-top: 5px;
            font-size: 25px;
            text-align: center;
            margin-bottom: 1px;
            &.active {
                color: yellow;
            }
        }
        p {
            width: 100%;
            height: 25px;
            line-height: 25px;
            font-size: 10px;
            font-weight: 400;
            color: #666;
            text-align: center;
        }
    }
    .box {
        flex: 1;
        display: flex;
        .look {
            border-radius: 3px;
            background-color: #3ca7fa;
            color: #fff;
            flex: 1;
            display: block;
            text-align: center;
            margin: 4px 0;
            line-height: 40px;
            font-size: 15px;
            font-weight: 500;
            color: #fff;
            align-items: center;
            justify-content: center;
        }
        .vip {
            border-radius: 3px 0 0 3px;
            background-color: #e2a500;
            color: #fff;
            flex: 1;
            text-align: center;
            margin: 4px 0;
            line-height: 40px;
            font-size: 15px;
            font-weight: 500;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .take {
            border-radius: 0 4px 4px 0;
            background-color: #ff5068;
            color: #fff;
            flex: 1;
            text-align: center;
            margin: 4px 0;
            line-height: 40px;
            font-size: 15px;
            font-weight: 500;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }
}
</style>
